﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PRE.Catalog
{
    public partial class FrmLophoc : PRE.Catalog.FrmBase
    {
        ServiceReference_Lophoc.WS_LophocSoapClient sr_lop = new ServiceReference_Lophoc.WS_LophocSoapClient();
        IList<ServiceReference_Lophoc.Lophoc> list_lop;

        public FrmLophoc()
        {
            InitializeComponent();
        }

        private void FrmLophoc_Load(object sender, EventArgs e)
        {
            PerformRefresh();
            
        }

        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {
            ReadOnlyControl(false);
            ClearDataBindings();
            ChangeStatus(false);
            ResetText();
            lbMsg.Visible = true;

            txtMalop.Focus();

            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            var id = Convert.ToInt32(grvMain.GetFocusedRowCellValue("id"));

            if (MessageBox.Show("Bạn có muốn xóa không ?", "Thông Báo", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                var ok = sr_lop.DeleteLophoc(id);
                if (ok)
                    PerformRefresh();
                else
                    MessageBox.Show("Lỗi! Vui lòng kiểm tra lại dữ liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);

            txtMalop.Properties.ReadOnly = true;
            txtTenlop.Focus();
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus();

            if (list_lop != null)
            {
                // Binding data
                ClearDataBindings();
                if (list_lop.Count > 0) DataBindingControl();
            }

            lbMsg.Visible = false;

            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            if (IsAdd)
            {
                if (txtMalop.Text == "" || txtTenlop.Text == "")
                {
                    MessageBox.Show("Bạn phải điền đầy đủ thông tin cho lớp học !!!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    PerformAdd();
                }
                else
                {
                    ServiceReference_Lophoc.Lophoc lp = new ServiceReference_Lophoc.Lophoc();

                    lp.malop = txtMalop.Text;
                    lp.tenlop = txtTenlop.Text;

                    var ok = sr_lop.InsertLophoc(lp);
                    if (ok)
                    {
                        PerformRefresh();
                        PerformAdd();
                        lbMsg.Text = " Lớp học " + lp.tenlop + " đã được thêm thành công";
                    }
                    else
                    {
                        MessageBox.Show("Lỗi! Vui lòng kiểm tra lại dữ liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txtMalop.Focus();
                    }
                }
            }

            if (IsEdit)
            {
                var _id = Convert.ToInt32(grvMain.GetFocusedRowCellValue("id"));

                ServiceReference_Lophoc.Lophoc lp = new ServiceReference_Lophoc.Lophoc();

                lp.id = _id;
                lp.tenlop = txtTenlop.Text;

                var ok = sr_lop.UpdateLophoc(lp);
                if (ok)
                {
                    PerformRefresh();
                    ReadOnlyControl(); ChangeStatus();
                    lbMsg.Text = " Lớp học " + lp.tenlop + " đã được sửa thông tin thành công";
                }
                else
                {
                    MessageBox.Show("Lỗi! Vui lòng kiểm tra lại dữ liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtTenlop.Focus();
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            txtMalop.Text = null;
            txtTenlop.Text = null;

            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txtMalop.DataBindings.Clear();
            txtTenlop.DataBindings.Clear();

            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
            txtMalop.DataBindings.Add("EditValue", list_lop, ".malop");
            txtTenlop.DataBindings.Add("EditValue", list_lop, ".tenlop");

            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            txtMalop.Properties.ReadOnly = isReadOnly;
            txtTenlop.Properties.ReadOnly = isReadOnly;

            grcMain.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {

            list_lop = (IList<ServiceReference_Lophoc.Lophoc>)sr_lop.SelectLophoc();

            if (list_lop != null)
                grcMain.DataSource = list_lop;

            base.LoadData();
        }

        #endregion

        private void txtMalop_TextChanged(object sender, EventArgs e)
        {
            ServiceReference_Lophoc.Lophoc lp = new ServiceReference_Lophoc.Lophoc();
            lp.malop = txtMalop.Text;
            IList<ServiceReference_Lophoc.Lophoc> tb2 = sr_lop.Check_Malop(lp);
            if (tb2.Count > 0)
            {
                lbMsg.Text = "Mã lớp đã tồn tại trong hệ thống, vui lòng nhập lại";
                txtMalop.Focus();
            }
            else
                lbMsg.Text = "";
        }

    }
}
